local wdtx__fireAttackSkill = fk.CreateSkill {
  name = "wdtx__fire_attack_skill",
}

wdtx__fireAttackSkill:addEffect("cardskill", {
  name = "wdtx__fire_attack_skill",
  prompt = "#wdtx__fire_attack_skill",
  target_num = 1,
  mod_target_filter = function(_, to_select, _, _, _, _)
    local to = to_select
    return not to:isKongcheng()
  end,
  target_filter = function(self, player, to_select, selected, selected_cards, card)
    if #selected < self:getMaxTargetNum(player, card) then
      return self:modTargetFilter(to_select, selected, player, card)
    end
  end,
  on_effect = function(self, room, cardEffectEvent)
    local from = cardEffectEvent.from
    local to = cardEffectEvent.to
    local mark = cardEffectEvent.card:getMark("@up_fire")
    if mark == 0 then
      if to:isKongcheng() then return end
      local showcard = room:askToCards(to,{
      min_num = 1,
      max_num = 1,
      include_equip = false,
      skill_name = "fire_attack_skill",
      cancelable = false,
      pattern = ".|.|.|hand",
      prompt = "#fire_attack-show:" .. from.id,
    })[1]
      to:showCards(showcard)
      local showCard = Fk:getCardById(showcard)
      local cards = room:askToDiscard(from, {
        min_num = 1,
        max_num = 1,
        include_equip = false,
        skill_name = "fire_attack_skill",
        cancelable = true,
        pattern = ".|.|" .. showCard:getSuitString(),
        prompt = "#fire_attack-discard:" .. to.id .. "::" .. showCard:getSuitString(),
      })
      if #cards > 0 then
        room:damage({
          from = from,
          to = to,
          card = cardEffectEvent.card,
          damage = 1,
          damageType = fk.FireDamage,
          skillName = wdtx__fireAttackSkill.name
        })
      end
    elseif mark > 0 then
      local firefunc = function()
        room:damage({
          from = from,
          to = to,
          card = cardEffectEvent.card,
          damage = 1,
          damageType = fk.FireDamage,
          skillName = wdtx__fireAttackSkill.name
        })
        room:addPlayerMark(to,"@up_fire",mark+1)
        if not to:hasSkill("burningSkill") then
          room:handleAddLoseSkills(to, "burningSkill", nil, false)
        end 
      end
      if mark < 3 then
        firefunc()
      elseif mark > 2 then
        firefunc()
        local firetars = {}
        local nextalive = to:getNextAlive()
        if nextalive and nextalive.role == to.role then
          table.insert(firetars,nextalive)
        end
        local lastar = table.find(room.alive_players, function (p)
          return p:getNextAlive() == to
        end)
        if lastar and lastar.role == to.role then
          table.insert(firetars,lastar)
        end
        for _, tar in ipairs(firetars) do
          room:damage({
            from = from,
            to = tar,
            -- card = cardEffectEvent.card,
            damage = 1,
            damageType = fk.FireDamage,
            skillName = wdtx__fireAttackSkill.name
          })
          room:addPlayerMark(tar,"@up_fire",2)
          if not tar:hasSkill("burningSkill") then
            room:handleAddLoseSkills(tar, "burningSkill", nil, false)
          end
        end
      end
    end
  end,
})


return wdtx__fireAttackSkill